﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.DigitalTwinAlignment.CApLifetimeManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.DigitalTwinAlignment;

internal class CApLifetimeManager
{
  private const string AppRegistryPath = "HKEY_CLASSES_ROOT\\SRAPP";
  private const string DefaultAppPath = "C:\\PSAR\\PSAR.exe";
  private CApSrServer _srServer;
  private Process _process;

  public bool IsRunning => this._srServer != null;

  public bool Start()
  {
    CApSrServer capSrServer = this.StartSrServer();
    if (!capSrServer.Send("plmxRequest"))
    {
      capSrServer.beamModel = true;
      if (!this.ActivateSkillRealApp())
        this.StopSrServer();
    }
    return this.IsRunning;
  }

  public void Stop() => this.StopSrServer();

  private CApSrServer StartSrServer()
  {
    if (this._srServer == null)
    {
      this._srServer = new CApSrServer();
      this.RegisterForEvents();
    }
    return this._srServer;
  }

  private void StopSrServer()
  {
    this.UnRegisterForEvents();
    this._srServer?.Stop();
    this._srServer = (CApSrServer) null;
    if (this._process != null)
      this._process.Exited -= new EventHandler(this.Process_Exited);
    this._process = (Process) null;
  }

  public bool IsSRRunning
  {
    get
    {
      Process process = this._process;
      return process != null && !process.HasExited;
    }
  }

  private void Process_Exited(object sender, EventArgs e) => this.StopSrServer();

  private void RegisterForEvents()
  {
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
    TxApplication.ApplicationEvents.Exiting += new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
  }

  private void UnRegisterForEvents()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
    TxApplication.ApplicationEvents.Exiting -= new TxApplication_ExitingEventHandler(this.ApplicationEvents_Exiting);
  }

  private void ActiveDocument_Unloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    this.StopSrServer();
  }

  private void ApplicationEvents_Exiting(object sender, TxApplication_ExitingEventArgs args)
  {
    this.StopSrServer();
  }

  private bool ActivateSkillRealApp()
  {
    string path = (string) Registry.GetValue("HKEY_CLASSES_ROOT\\SRAPP", "", (object) "C:\\PSAR\\PSAR.exe");
    string directoryName = Path.GetDirectoryName(path);
    this._process = new Process()
    {
      StartInfo = {
        FileName = path,
        WorkingDirectory = string.IsNullOrWhiteSpace(directoryName) ? "" : directoryName,
        WindowStyle = ProcessWindowStyle.Maximized
      },
      EnableRaisingEvents = true
    };
    try
    {
      this._process.Start();
      this._process.Exited += new EventHandler(this.Process_Exited);
      return true;
    }
    catch
    {
    }
    return false;
  }
}
